package com.lft.com.oilgas.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lft.com.oilgas.entity.Commodity;
import com.lft.com.oilgas.mapper.CommodityMapper;
import com.lft.com.oilgas.service.ICommodityService;
import com.lft.com.oilgas.util.QueryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tyqw
 * @since 2024-09-10
 */
@Service
public class CommodityServiceImpl implements ICommodityService {
    @Autowired
    private CommodityMapper commodityMapper;
    //添加或修改
    @Override
    public int insterOrupdateCommodity(Commodity commodity) {
        if (commodity.getId() == null) {
            return commodityMapper.insert(commodity);
        } else {
            return commodityMapper.updateById(commodity);
        }
    }

    //分页查询
    @Override
    public List<Commodity> getCommodities(QueryBean queryBean) {
        LambdaQueryWrapper<Commodity> queryWrapper = new LambdaQueryWrapper<>();
        Page<Commodity> page = new Page<>(queryBean.getPageNo(), queryBean.getPageSize());
        // 模糊查询商品名称
        if (queryBean.getConditions() != null) {
            queryWrapper.like(Commodity::getName, queryBean.getConditions());
        }
        // 精确匹配商品类型
        if (queryBean.getProductType() != null) {
            queryWrapper.eq(Commodity::getProductType, queryBean.getProductType());
        }

        Page<Commodity> resultPage = commodityMapper.selectPage(page, queryWrapper);
        return resultPage.getRecords();
    }

    @Override
    public List<Commodity> bymerchantid(Integer id) {
        return commodityMapper.selectByMerchantId(id);
    }

    @Override
    public Commodity selectByid(Integer id) {
        return   commodityMapper.selectById(id);
    }

    @Override
    public void insertOrUpdate(Commodity commodity) {
        if(commodity.getId()!=null){
            commodityMapper.updateById(commodity);
        }else {
            commodityMapper.insert(commodity);
        }
    }

    @Override
    public void delectById(Integer id) {
        commodityMapper.deleteById(id);
    }
}
